from flask import jsonify
from sqlalchemy import text

from tzq.database_compare_system import dcs_bp
from tzq.extensions import db


@dcs_bp.route('/api/initJobTableColumnRepare/<int:id>', methods=['GET'])
def init_job_table_column_repare(id):
    """
        初始化 模型比对修复 表数据
        1、比对任务，把 “模型字段比对修复” 的结果插入到 dc_job_table_column_repare_t 表
    """
    try:
        v_sql = text("""
delete from dc_job_table_column_repare_t where comp_task_id = :id ;
insert into dc_job_table_column_repare_t (
comp_task_id, comp_task_name, schema, table_name, column_name, total_check_result, table_name_check_result, table_name1, table_name2, table_name3, table_name4, table_name5, table_name6, table_name7, column_name_check_result, column_name1, column_name2, column_name2_repair_script, column_name3, column_name3_repair_script, column_name4, column_name4_repair_script, column_name5, column_name5_repair_script, column_name6, column_name6_repair_script, column_name7, column_name7_repair_script, data_type_check_result, data_type1, data_type2, data_type2_repair_script, data_type3, data_type3_repair_script, data_type4, data_type4_repair_script, data_type5, data_type5_repair_script, data_type6, data_type6_repair_script, data_type7, data_type7_repair_script, data_default_check_result, data_default1, data_default2, data_default2_repair_script, data_default3, data_default3_repair_script, data_default4, data_default4_repair_script, data_default5, data_default5_repair_script, data_default6, data_default6_repair_script, data_default7, data_default7_repair_script, pkey_check_result, pkey1, pkey2, pkey2_repair_script, pkey3, pkey3_repair_script, pkey4, pkey4_repair_script, pkey5, pkey5_repair_script, pkey6, pkey6_repair_script, pkey7, pkey7_repair_script, nullable_check_result, nullable1, nullable2, nullable2_repair_script, nullable3, nullable3_repair_script, nullable4, nullable4_repair_script, nullable5, nullable5_repair_script, nullable6, nullable6_repair_script, nullable7, nullable7_repair_script, table_comment_check_result, table_comment1, table_comment2, table_comment2_repair_script, table_comment3, table_comment3_repair_script, table_comment4, table_comment4_repair_script, table_comment5, table_comment5_repair_script, table_comment6, table_comment6_repair_script, table_comment7, table_comment7_repair_script, column_comment_check_result, column_comment1, column_comment2, column_comment2_repair_script, column_comment3, column_comment3_repair_script, column_comment4, column_comment4_repair_script, column_comment5, column_comment5_repair_script, column_comment6, column_comment6_repair_script, column_comment7, column_comment7_repair_script
)
select t.comp_task_id
			,t.comp_task_name
			,t.schema
			,t.table_name
      ,t.column_name
      ,t.total_check_result
			,t.table_name_check_result
			,t.table_name1
			,t.table_name2
			,table_name3
			,table_name4
			,table_name5,table_name6,table_name7
			,column_name_check_result
			,column_name1
			,column_name2
			,case
         when t.total_check_result = '×' and t.table_name_check_result = '√' and t.column_name_check_result = '×' 
					and t.column_name1 = '√' and t.column_name2 = '×'
           then 'alter table '||t.schema||'.'||t.table_name|| ' add column ' || t.column_name || ' ' || data_type1 || ';'  
         when t.total_check_result = '×' and t.table_name_check_result = '√' and t.column_name_check_result = '×' 
					and t.column_name1 = '×' and t.column_name2 = '√'
					 then 'alter table '||t.schema||'.'||t.table_name|| ' drop column ' || t.column_name || ';'  
				 else null 
       end as column_name2_repair_script
			,column_name3
			,case
         when t.total_check_result = '×' and t.table_name_check_result = '√' and t.column_name_check_result = '×' 
					and t.column_name1 = '√' and t.column_name3 = '×'
           then 'alter table '||t.schema||'.'||t.table_name|| ' add column ' || t.column_name || ' ' || data_type1 || ';'  
         when t.total_check_result = '×' and t.table_name_check_result = '√' and t.column_name_check_result = '×' 
					and t.column_name1 = '×' and t.column_name3 = '√'
					 then 'alter table '||t.schema||'.'||t.table_name|| ' drop column ' || t.column_name || ';'  
				 else null 
       end as column_name3_repair_script
			,column_name4
			,case
         when t.total_check_result = '×' and t.table_name_check_result = '√' and t.column_name_check_result = '×' 
					and t.column_name1 = '√' and t.column_name4 = '×'
           then 'alter table '||t.schema||'.'||t.table_name|| ' add column ' || t.column_name || ' ' || data_type1 || ';'  
         when t.total_check_result = '×' and t.table_name_check_result = '√' and t.column_name_check_result = '×' 
					and t.column_name1 = '×' and t.column_name4 = '√'
					 then 'alter table '||t.schema||'.'||t.table_name|| ' drop column ' || t.column_name || ';'  
				 else null 
       end as column_name4_repair_script
			,column_name5
			,case
         when t.total_check_result = '×' and t.table_name_check_result = '√' and t.column_name_check_result = '×' 
					and t.column_name1 = '√' and t.column_name5 = '×'
           then 'alter table '||t.schema||'.'||t.table_name|| ' add column ' || t.column_name || ' ' || data_type1 || ';'  
         when t.total_check_result = '×' and t.table_name_check_result = '√' and t.column_name_check_result = '×' 
					and t.column_name1 = '×' and t.column_name5 = '√'
					 then 'alter table '||t.schema||'.'||t.table_name|| ' drop column ' || t.column_name || ';'  
				 else null 
       end as column_name5_repair_script
			,column_name6
			,case
         when t.total_check_result = '×' and t.table_name_check_result = '√' and t.column_name_check_result = '×' 
					and t.column_name1 = '√' and t.column_name6 = '×'
           then 'alter table '||t.schema||'.'||t.table_name|| ' add column ' || t.column_name || ' ' || data_type1 || ';'  
         when t.total_check_result = '×' and t.table_name_check_result = '√' and t.column_name_check_result = '×' 
					and t.column_name1 = '×' and t.column_name6 = '√'
					 then 'alter table '||t.schema||'.'||t.table_name|| ' drop column ' || t.column_name || ';'  
				 else null 
       end as column_name6_repair_script
			,column_name7
			,case
         when t.total_check_result = '×' and t.table_name_check_result = '√' and t.column_name_check_result = '×' 
					and t.column_name1 = '√' and t.column_name7 = '×'
           then 'alter table '||t.schema||'.'||t.table_name|| ' add column ' || t.column_name || ' ' || data_type1 || ';'  
         when t.total_check_result = '×' and t.table_name_check_result = '√' and t.column_name_check_result = '×' 
					and t.column_name1 = '×' and t.column_name7 = '√'
					 then 'alter table '||t.schema||'.'||t.table_name|| ' drop column ' || t.column_name || ';'  
				 else null 
       end as column_name7_repair_script
			,data_type_check_result
			,data_type1
			,data_type2
			,case
         when t.total_check_result = '×' and t.table_name_check_result = '√' and t.column_name_check_result = '√' 
					and t.data_type_check_result = '×' and t.data_type1 != t.data_type2
           then 'alter table '||t.schema||'.'||t.table_name|| ' alter column ' || t.column_name ||
                ' set data type ' || data_type1 || ';' 
				 else null 
       end as data_type2_repair_script
			,data_type3
			,case
         when t.total_check_result = '×' and t.table_name_check_result = '√' and t.column_name_check_result = '√' 
					and t.data_type_check_result = '×' and t.data_type1 != t.data_type3
           then 'alter table '||t.schema||'.'||t.table_name|| ' alter column ' || t.column_name ||
                ' set data type ' || data_type1 || ';' 
				 else null 
       end as data_type3_repair_script
			,data_type4
			,case
         when t.total_check_result = '×' and t.table_name_check_result = '√' and t.column_name_check_result = '√' 
					and t.data_type_check_result = '×' and t.data_type1 != t.data_type4
           then 'alter table '||t.schema||'.'||t.table_name|| ' alter column ' || t.column_name ||
                ' set data type ' || data_type1 || ';' 
				 else null 
       end as data_type4_repair_script
			,data_type5
			,case
         when t.total_check_result = '×' and t.table_name_check_result = '√' and t.column_name_check_result = '√' 
					and t.data_type_check_result = '×' and t.data_type1 != t.data_type5
           then 'alter table '||t.schema||'.'||t.table_name|| ' alter column ' || t.column_name ||
                ' set data type ' || data_type1 || ';' 
				 else null 
       end as data_type5_repair_script
			,data_type6
			,case
         when t.total_check_result = '×' and t.table_name_check_result = '√' and t.column_name_check_result = '√' 
					and t.data_type_check_result = '×' and t.data_type1 != t.data_type6
           then 'alter table '||t.schema||'.'||t.table_name|| ' alter column ' || t.column_name ||
                ' set data type ' || data_type1 || ';' 
				 else null 
       end as data_type6_repair_script
			,data_type7
			,case
         when t.total_check_result = '×' and t.table_name_check_result = '√' and t.column_name_check_result = '√' 
					and t.data_type_check_result = '×' and t.data_type1 != t.data_type7
           then 'alter table '||t.schema||'.'||t.table_name|| ' alter column ' || t.column_name ||
                ' set data type ' || data_type1 || ';' 
				 else null 
       end as data_type7_repair_script
			,data_default_check_result
			,data_default1,data_default2
			,case
         when t.total_check_result = '×' and t.table_name_check_result = '√' and t.column_name_check_result = '√' 
					and t.data_default_check_result = '×' and COALESCE(t.data_default1,'tzqnull') != COALESCE(t.data_default2,'tzqnull')
           then 'alter table '||t.schema||'.'||t.table_name|| ' alter column ' || t.column_name ||
                ' set DEFAULT ' || data_default1 || ';' 
				 else null 
       end as data_default2_repair_script
			,data_default3
			,case
         when t.total_check_result = '×' and t.table_name_check_result = '√' and t.column_name_check_result = '√' 
					and t.data_default_check_result = '×' and COALESCE(t.data_default1,'tzqnull') != COALESCE(t.data_default3,'tzqnull')
           then 'alter table '||t.schema||'.'||t.table_name|| ' alter column ' || t.column_name ||
                ' set DEFAULT ' || data_default1 || ';' 
				 else null 
       end as data_default3_repair_script
			,data_default4
			,case
         when t.total_check_result = '×' and t.table_name_check_result = '√' and t.column_name_check_result = '√' 
					and t.data_default_check_result = '×' and COALESCE(t.data_default1,'tzqnull') != COALESCE(t.data_default4,'tzqnull')
           then 'alter table '||t.schema||'.'||t.table_name|| ' alter column ' || t.column_name ||
                ' set DEFAULT ' || data_default1 || ';' 
				 else null 
       end as data_default4_repair_script
			,data_default5
			,case
         when t.total_check_result = '×' and t.table_name_check_result = '√' and t.column_name_check_result = '√' 
					and t.data_default_check_result = '×' and COALESCE(t.data_default1,'tzqnull') != COALESCE(t.data_default5,'tzqnull')
           then 'alter table '||t.schema||'.'||t.table_name|| ' alter column ' || t.column_name ||
                ' set DEFAULT ' || data_default1 || ';' 
				 else null 
       end as data_default5_repair_script
			,data_default6
			,case
         when t.total_check_result = '×' and t.table_name_check_result = '√' and t.column_name_check_result = '√' 
					and t.data_default_check_result = '×' and COALESCE(t.data_default1,'tzqnull') != COALESCE(t.data_default6,'tzqnull')
           then 'alter table '||t.schema||'.'||t.table_name|| ' alter column ' || t.column_name ||
                ' set DEFAULT ' || data_default1 || ';' 
				 else null 
       end as data_default6_repair_script
			,data_default7
			,case
         when t.total_check_result = '×' and t.table_name_check_result = '√' and t.column_name_check_result = '√' 
					and t.data_default_check_result = '×' and COALESCE(t.data_default1,'tzqnull') != COALESCE(t.data_default7,'tzqnull')
           then 'alter table '||t.schema||'.'||t.table_name|| ' alter column ' || t.column_name ||
                ' set DEFAULT ' || data_default1 || ';' 
				 else null 
       end as data_default7_repair_script
			,pkey_check_result
			,pkey1,pkey2
			,case
         when t.total_check_result = '×' and t.table_name_check_result = '√' and t.column_name_check_result = '√' 
					and t.pkey_check_result = '×' and t.pkey1 = 'Y'
					and COALESCE(t.pkey1,'tzqnull') != COALESCE(t.pkey2,'tzqnull')
           then 'alter table '||t.schema||'.'||t.table_name|| ' ADD PRIMARY KEY (' || t.column_name || ');' 
				 when t.total_check_result = '×' and t.table_name_check_result = '√' and t.column_name_check_result = '√' 
					and t.pkey_check_result = '×' and (t.pkey1 is null or t.pkey1 = 'N')
					and COALESCE(t.pkey1,'tzqnull') != COALESCE(t.pkey2,'tzqnull')
           then 'alter table '||t.schema||'.'||t.table_name|| ' DROP CONSTRAINT ' || t.column_name || ';'
				 else null 
       end as pkey2_repair_script
			,pkey3
			,case
         when t.total_check_result = '×' and t.table_name_check_result = '√' and t.column_name_check_result = '√' 
					and t.pkey_check_result = '×' and t.pkey1 = 'Y'
					and COALESCE(t.pkey1,'tzqnull') != COALESCE(t.pkey3,'tzqnull')
           then 'alter table '||t.schema||'.'||t.table_name|| ' ADD PRIMARY KEY (' || t.column_name || ');' 
				 when t.total_check_result = '×' and t.table_name_check_result = '√' and t.column_name_check_result = '√' 
					and t.pkey_check_result = '×' and (t.pkey1 is null or t.pkey1 = 'N')
					and COALESCE(t.pkey1,'tzqnull') != COALESCE(t.pkey3,'tzqnull')
           then 'alter table '||t.schema||'.'||t.table_name|| ' DROP CONSTRAINT ' || t.column_name || ';'
				 else null 
       end as pkey3_repair_script
			,pkey4
			,case
         when t.total_check_result = '×' and t.table_name_check_result = '√' and t.column_name_check_result = '√' 
					and t.pkey_check_result = '×' and t.pkey1 = 'Y'
					and COALESCE(t.pkey1,'tzqnull') != COALESCE(t.pkey4,'tzqnull')
           then 'alter table '||t.schema||'.'||t.table_name|| ' ADD PRIMARY KEY (' || t.column_name || ');' 
				 when t.total_check_result = '×' and t.table_name_check_result = '√' and t.column_name_check_result = '√' 
					and t.pkey_check_result = '×' and (t.pkey1 is null or t.pkey1 = 'N')
					and COALESCE(t.pkey1,'tzqnull') != COALESCE(t.pkey4,'tzqnull')
           then 'alter table '||t.schema||'.'||t.table_name|| ' DROP CONSTRAINT ' || t.column_name || ';'
				 else null 
       end as pkey4_repair_script
			,pkey5
			,case
         when t.total_check_result = '×' and t.table_name_check_result = '√' and t.column_name_check_result = '√' 
					and t.pkey_check_result = '×' and t.pkey1 = 'Y'
					and COALESCE(t.pkey1,'tzqnull') != COALESCE(t.pkey5,'tzqnull')
           then 'alter table '||t.schema||'.'||t.table_name|| ' ADD PRIMARY KEY (' || t.column_name || ');' 
				 when t.total_check_result = '×' and t.table_name_check_result = '√' and t.column_name_check_result = '√' 
					and t.pkey_check_result = '×' and (t.pkey1 is null or t.pkey1 = 'N')
					and COALESCE(t.pkey1,'tzqnull') != COALESCE(t.pkey5,'tzqnull')
           then 'alter table '||t.schema||'.'||t.table_name|| ' DROP CONSTRAINT ' || t.column_name || ';'
				 else null 
       end as pkey5_repair_script
			,pkey6
			,case
         when t.total_check_result = '×' and t.table_name_check_result = '√' and t.column_name_check_result = '√' 
					and t.pkey_check_result = '×' and t.pkey1 = 'Y'
					and COALESCE(t.pkey1,'tzqnull') != COALESCE(t.pkey6,'tzqnull')
           then 'alter table '||t.schema||'.'||t.table_name|| ' ADD PRIMARY KEY (' || t.column_name || ');' 
				 when t.total_check_result = '×' and t.table_name_check_result = '√' and t.column_name_check_result = '√' 
					and t.pkey_check_result = '×' and (t.pkey1 is null or t.pkey1 = 'N')
					and COALESCE(t.pkey1,'tzqnull') != COALESCE(t.pkey6,'tzqnull')
           then 'alter table '||t.schema||'.'||t.table_name|| ' DROP CONSTRAINT ' || t.column_name || ';'
				 else null 
       end as pkey6_repair_script
			,pkey7
			,case
         when t.total_check_result = '×' and t.table_name_check_result = '√' and t.column_name_check_result = '√' 
					and t.pkey_check_result = '×' and t.pkey1 = 'Y'
					and COALESCE(t.pkey1,'tzqnull') != COALESCE(t.pkey7,'tzqnull')
           then 'alter table '||t.schema||'.'||t.table_name|| ' ADD PRIMARY KEY (' || t.column_name || ');' 
				 when t.total_check_result = '×' and t.table_name_check_result = '√' and t.column_name_check_result = '√' 
					and t.pkey_check_result = '×' and (t.pkey1 is null or t.pkey1 = 'N')
					and COALESCE(t.pkey1,'tzqnull') != COALESCE(t.pkey7,'tzqnull')
           then 'alter table '||t.schema||'.'||t.table_name|| ' DROP CONSTRAINT ' || t.column_name || ';'
				 else null 
       end as pkey7_repair_script
			,nullable_check_result
			,nullable1,nullable2
			,case
         when t.total_check_result = '×' and t.table_name_check_result = '√' and t.column_name_check_result = '√' 
					and t.nullable_check_result = '×' and t.nullable1 = 'NO' and t.nullable2 = 'YES'
           then 'alter table '||t.schema||'.'||t.table_name|| ' alter column ' || t.column_name || ' SET NOT NULL;' 
				 when t.total_check_result = '×' and t.table_name_check_result = '√' and t.column_name_check_result = '√' 
					and t.nullable_check_result = '×' and t.nullable1 = 'YES' and t.nullable2 = 'NO'
           then 'alter table '||t.schema||'.'||t.table_name|| ' alter column ' || t.column_name || ' DROP NOT NULL;' 
				 else null 
       end as nullable2_repair_script
			,nullable3
			,case
         when t.total_check_result = '×' and t.table_name_check_result = '√' and t.column_name_check_result = '√' 
					and t.nullable_check_result = '×' and t.nullable1 = 'NO' and t.nullable3 = 'YES'
           then 'alter table '||t.schema||'.'||t.table_name|| ' alter column ' || t.column_name || ' SET NOT NULL;' 
				 when t.total_check_result = '×' and t.table_name_check_result = '√' and t.column_name_check_result = '√' 
					and t.nullable_check_result = '×' and t.nullable1 = 'YES' and t.nullable3 = 'NO'
           then 'alter table '||t.schema||'.'||t.table_name|| ' alter column ' || t.column_name || ' DROP NOT NULL;' 
				 else null 
       end as nullable3_repair_script
			,nullable4
			,case
         when t.total_check_result = '×' and t.table_name_check_result = '√' and t.column_name_check_result = '√' 
					and t.nullable_check_result = '×' and t.nullable1 = 'NO' and t.nullable4 = 'YES'
           then 'alter table '||t.schema||'.'||t.table_name|| ' alter column ' || t.column_name || ' SET NOT NULL;' 
				 when t.total_check_result = '×' and t.table_name_check_result = '√' and t.column_name_check_result = '√' 
					and t.nullable_check_result = '×' and t.nullable1 = 'YES' and t.nullable4 = 'NO'
           then 'alter table '||t.schema||'.'||t.table_name|| ' alter column ' || t.column_name || ' DROP NOT NULL;' 
				 else null 
       end as nullable4_repair_script
			,nullable5
			,case
         when t.total_check_result = '×' and t.table_name_check_result = '√' and t.column_name_check_result = '√' 
					and t.nullable_check_result = '×' and t.nullable1 = 'NO' and t.nullable5 = 'YES'
           then 'alter table '||t.schema||'.'||t.table_name|| ' alter column ' || t.column_name || ' SET NOT NULL;' 
				 when t.total_check_result = '×' and t.table_name_check_result = '√' and t.column_name_check_result = '√' 
					and t.nullable_check_result = '×' and t.nullable1 = 'YES' and t.nullable5 = 'NO'
           then 'alter table '||t.schema||'.'||t.table_name|| ' alter column ' || t.column_name || ' DROP NOT NULL;' 
				 else null 
       end as nullable5_repair_script
			,nullable6
			,case
         when t.total_check_result = '×' and t.table_name_check_result = '√' and t.column_name_check_result = '√' 
					and t.nullable_check_result = '×' and t.nullable1 = 'NO' and t.nullable6 = 'YES'
           then 'alter table '||t.schema||'.'||t.table_name|| ' alter column ' || t.column_name || ' SET NOT NULL;' 
				 when t.total_check_result = '×' and t.table_name_check_result = '√' and t.column_name_check_result = '√' 
					and t.nullable_check_result = '×' and t.nullable1 = 'YES' and t.nullable6 = 'NO'
           then 'alter table '||t.schema||'.'||t.table_name|| ' alter column ' || t.column_name || ' DROP NOT NULL;' 
				 else null 
       end as nullable6_repair_script
			,nullable7
			,case
         when t.total_check_result = '×' and t.table_name_check_result = '√' and t.column_name_check_result = '√' 
					and t.nullable_check_result = '×' and t.nullable1 = 'NO' and t.nullable7 = 'YES'
           then 'alter table '||t.schema||'.'||t.table_name|| ' alter column ' || t.column_name || ' SET NOT NULL;' 
				 when t.total_check_result = '×' and t.table_name_check_result = '√' and t.column_name_check_result = '√' 
					and t.nullable_check_result = '×' and t.nullable1 = 'YES' and t.nullable7 = 'NO'
           then 'alter table '||t.schema||'.'||t.table_name|| ' alter column ' || t.column_name || ' DROP NOT NULL;' 
				 else null 
       end as nullable7_repair_script
			,table_comment_check_result
			,table_comment1, table_comment2
			,case
         when t.total_check_result = '×' and t.table_name_check_result = '√' and t.column_name_check_result = '√' 
					and t.table_comment_check_result = '×' and t.table_comment1 is not null
					and COALESCE(t.table_comment1,'tzqnull') != COALESCE(t.table_comment2,'tzqnull')
           then 'COMMENT ON TABLE '||t.schema||'.'||t.table_name|| ' IS ''' || t.table_comment1 || ''';' 
				 when t.total_check_result = '×' and t.table_name_check_result = '√' and t.column_name_check_result = '√' 
					and t.table_comment_check_result = '×' and t.table_comment1 is null
           then 'COMMENT ON TABLE '||t.schema||'.'||t.table_name|| ' IS '''';' 
				 else null
       end as table_comment2_repair_script
			,table_comment3
			,case
         when t.total_check_result = '×' and t.table_name_check_result = '√' and t.column_name_check_result = '√' 
					and t.table_comment_check_result = '×' and t.table_comment1 is not null
					and COALESCE(t.table_comment1,'tzqnull') != COALESCE(t.table_comment3,'tzqnull')
           then 'COMMENT ON TABLE '||t.schema||'.'||t.table_name|| ' IS ''' || t.table_comment1 || ''';' 
				 when t.total_check_result = '×' and t.table_name_check_result = '√' and t.column_name_check_result = '√' 
					and t.table_comment_check_result = '×' and t.table_comment1 is null
					and COALESCE(t.table_comment1,'tzqnull') != COALESCE(t.table_comment3,'tzqnull')
           then 'COMMENT ON TABLE '||t.schema||'.'||t.table_name|| ' IS '''';' 
				 else null
       end as table_comment3_repair_script
			,table_comment4
			,case
         when t.total_check_result = '×' and t.table_name_check_result = '√' and t.column_name_check_result = '√' 
					and t.table_comment_check_result = '×' and t.table_comment1 is not null
					and COALESCE(t.table_comment1,'tzqnull') != COALESCE(t.table_comment4,'tzqnull')
           then 'COMMENT ON TABLE '||t.schema||'.'||t.table_name|| ' IS ''' || t.table_comment1 || ''';' 
				 when t.total_check_result = '×' and t.table_name_check_result = '√' and t.column_name_check_result = '√' 
					and t.table_comment_check_result = '×' and t.table_comment1 is null
					and COALESCE(t.table_comment1,'tzqnull') != COALESCE(t.table_comment4,'tzqnull')
           then 'COMMENT ON TABLE '||t.schema||'.'||t.table_name|| ' IS '''';' 
				 else null
       end as table_comment4_repair_script
			,table_comment5
			,case
         when t.total_check_result = '×' and t.table_name_check_result = '√' and t.column_name_check_result = '√' 
					and t.table_comment_check_result = '×' and t.table_comment1 is not null
					and COALESCE(t.table_comment1,'tzqnull') != COALESCE(t.table_comment5,'tzqnull')
           then 'COMMENT ON TABLE '||t.schema||'.'||t.table_name|| ' IS ''' || t.table_comment1 || ''';' 
				 when t.total_check_result = '×' and t.table_name_check_result = '√' and t.column_name_check_result = '√' 
					and t.table_comment_check_result = '×' and t.table_comment1 is null
					and COALESCE(t.table_comment1,'tzqnull') != COALESCE(t.table_comment5,'tzqnull')
           then 'COMMENT ON TABLE '||t.schema||'.'||t.table_name|| ' IS '''';' 
				 else null
       end as table_comment5_repair_script
			,table_comment6
			,case
         when t.total_check_result = '×' and t.table_name_check_result = '√' and t.column_name_check_result = '√' 
					and t.table_comment_check_result = '×' and t.table_comment1 is not null
					and COALESCE(t.table_comment1,'tzqnull') != COALESCE(t.table_comment6,'tzqnull')
           then 'COMMENT ON TABLE '||t.schema||'.'||t.table_name|| ' IS ''' || t.table_comment1 || ''';' 
				 when t.total_check_result = '×' and t.table_name_check_result = '√' and t.column_name_check_result = '√' 
					and t.table_comment_check_result = '×' and t.table_comment1 is null
					and COALESCE(t.table_comment1,'tzqnull') != COALESCE(t.table_comment6,'tzqnull')
           then 'COMMENT ON TABLE '||t.schema||'.'||t.table_name|| ' IS '''';' 
				 else null
       end as table_comment6_repair_script
			,table_comment7
			,case
         when t.total_check_result = '×' and t.table_name_check_result = '√' and t.column_name_check_result = '√' 
					and t.table_comment_check_result = '×' and t.table_comment1 is not null
					and COALESCE(t.table_comment1,'tzqnull') != COALESCE(t.table_comment7,'tzqnull')
           then 'COMMENT ON TABLE '||t.schema||'.'||t.table_name|| ' IS ''' || t.table_comment1 || ''';' 
				 when t.total_check_result = '×' and t.table_name_check_result = '√' and t.column_name_check_result = '√' 
					and t.table_comment_check_result = '×' and t.table_comment1 is null
					and COALESCE(t.table_comment1,'tzqnull') != COALESCE(t.table_comment7,'tzqnull')
           then 'COMMENT ON TABLE '||t.schema||'.'||t.table_name|| ' IS '''';' 
				 else null
       end as table_comment7_repair_script
			,column_comment_check_result,column_comment1, column_comment2
			,case
         when t.total_check_result = '×' and t.table_name_check_result = '√' and t.column_name_check_result = '√' 
					and t.column_comment_check_result = '×' and t.column_comment1 is not null
					and COALESCE(t.column_comment1,'tzqnull') != COALESCE(t.column_comment2,'tzqnull')
           then 'COMMENT ON COLUMN '||t.table_name||'.'||t.column_name|| ' IS ''' || t.column_comment1 || ''';' 
				 when t.total_check_result = '×' and t.table_name_check_result = '√' and t.column_name_check_result = '√' 
					and t.column_comment_check_result = '×' and t.column_comment1 is null
					and COALESCE(t.column_comment1,'tzqnull') != COALESCE(t.column_comment2,'tzqnull')
           then 'COMMENT ON COLUMN '||t.table_name||'.'||t.column_name|| ' IS '''';' 
				 else null
       end as column_comment2_repair_script
			,column_comment3
			,case
         when t.total_check_result = '×' and t.table_name_check_result = '√' and t.column_name_check_result = '√' 
					and t.column_comment_check_result = '×' and t.column_comment1 is not null
					and COALESCE(t.column_comment1,'tzqnull') != COALESCE(t.column_comment3,'tzqnull')
           then 'COMMENT ON COLUMN '||t.table_name||'.'||t.column_name|| ' IS ''' || t.column_comment1 || ''';' 
				 when t.total_check_result = '×' and t.table_name_check_result = '√' and t.column_name_check_result = '√' 
					and t.column_comment_check_result = '×' and t.column_comment1 is null
					and COALESCE(t.column_comment1,'tzqnull') != COALESCE(t.column_comment3,'tzqnull')
           then 'COMMENT ON COLUMN '||t.table_name||'.'||t.column_name|| ' IS '''';' 
				 else null
       end as column_comment3_repair_script
			,column_comment4
			,case
         when t.total_check_result = '×' and t.table_name_check_result = '√' and t.column_name_check_result = '√' 
					and t.column_comment_check_result = '×' and t.column_comment1 is not null
					and COALESCE(t.column_comment1,'tzqnull') != COALESCE(t.column_comment4,'tzqnull')
           then 'COMMENT ON COLUMN '||t.table_name||'.'||t.column_name|| ' IS ''' || t.column_comment1 || ''';' 
				 when t.total_check_result = '×' and t.table_name_check_result = '√' and t.column_name_check_result = '√' 
					and t.column_comment_check_result = '×' and t.column_comment1 is null
					and COALESCE(t.column_comment1,'tzqnull') != COALESCE(t.column_comment4,'tzqnull')
           then 'COMMENT ON COLUMN '||t.table_name||'.'||t.column_name|| ' IS '''';' 
				 else null
       end as column_comment4_repair_script
			,column_comment5
			,case
         when t.total_check_result = '×' and t.table_name_check_result = '√' and t.column_name_check_result = '√' 
					and t.column_comment_check_result = '×' and t.column_comment1 is not null
					and COALESCE(t.column_comment1,'tzqnull') != COALESCE(t.column_comment5,'tzqnull')
           then 'COMMENT ON COLUMN '||t.table_name||'.'||t.column_name|| ' IS ''' || t.column_comment1 || ''';' 
				 when t.total_check_result = '×' and t.table_name_check_result = '√' and t.column_name_check_result = '√' 
					and t.column_comment_check_result = '×' and t.column_comment1 is null
					and COALESCE(t.column_comment1,'tzqnull') != COALESCE(t.column_comment5,'tzqnull')
           then 'COMMENT ON COLUMN '||t.table_name||'.'||t.column_name|| ' IS '''';' 
				 else null
       end as column_comment5_repair_script
			,column_comment6
			,case
         when t.total_check_result = '×' and t.table_name_check_result = '√' and t.column_name_check_result = '√' 
					and t.column_comment_check_result = '×' and t.column_comment1 is not null
					and COALESCE(t.column_comment1,'tzqnull') != COALESCE(t.column_comment6,'tzqnull')
           then 'COMMENT ON COLUMN '||t.table_name||'.'||t.column_name|| ' IS ''' || t.column_comment1 || ''';' 
				 when t.total_check_result = '×' and t.table_name_check_result = '√' and t.column_name_check_result = '√' 
					and t.column_comment_check_result = '×' and t.column_comment1 is null
					and COALESCE(t.column_comment1,'tzqnull') != COALESCE(t.column_comment6,'tzqnull')
           then 'COMMENT ON COLUMN '||t.table_name||'.'||t.column_name|| ' IS '''';' 
				 else null
       end as column_comment6_repair_script
			,column_comment7
			,case
         when t.total_check_result = '×' and t.table_name_check_result = '√' and t.column_name_check_result = '√' 
					and t.column_comment_check_result = '×' and t.column_comment1 is not null
					and COALESCE(t.column_comment1,'tzqnull') != COALESCE(t.column_comment7,'tzqnull')
           then 'COMMENT ON COLUMN '||t.table_name||'.'||t.column_name|| ' IS ''' || t.column_comment1 || ''';' 
				 when t.total_check_result = '×' and t.table_name_check_result = '√' and t.column_name_check_result = '√' 
					and t.column_comment_check_result = '×' and t.column_comment1 is null
					and COALESCE(t.column_comment1,'tzqnull') != COALESCE(t.column_comment7,'tzqnull')
           then 'COMMENT ON COLUMN '||t.table_name||'.'||t.column_name|| ' IS '''';' 
				 else null
       end as column_comment7_repair_script
  from dc_comp_tab_job_result_t t
	left join dc_comp_task_t ct
	  on t.comp_task_id = ct.comp_task_id
	left join dc_database_info_t di1 on ct.db_info_1_id = di1.db_info_id
	left join dc_table_define_t  td1 
	  on td1.source = di1.connect_name
	 and td1.schema = t.schema
	 and td1.table_name = t.table_name
 where 1=1
   and t.table_name !~* '[0-9]$|\_bak$|^act\_|^apw|^erp|^msi'
   and t.comp_task_id = :id
;
            """)
        db.session.execute(v_sql, {
            'id': id
        })
        db.session.commit()
        return jsonify({"code": 1, "msg": "【OK】"})
    except Exception as e:
        db.session.rollback()
        return jsonify({"code": 0, "msg": str(e), "error": str(e)}), 500
